package com.sincon.system.controller;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sincon.common.annotation.Log;
import com.sincon.common.core.controller.BaseController;
import com.sincon.common.core.domain.AjaxResult;
import com.sincon.common.enums.BusinessType;
import com.sincon.system.domain.TbServerInfo;
import com.sincon.system.service.ITbServerInfoService;
import com.sincon.common.utils.poi.ExcelUtil;
import com.sincon.common.core.page.TableDataInfo;

/**
 * 服务器信息主Controller
 * 
 * @author LaoGao
 * @date 2024-12-23
 */
@RestController
@RequestMapping("/system/info")
public class TbServerInfoController extends BaseController
{
    @Autowired
    private ITbServerInfoService tbServerInfoService;

    /**
     * 查询服务器信息主列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbServerInfo tbServerInfo)
    {
        startPage();
        List<TbServerInfo> list = tbServerInfoService.selectTbServerInfoList(tbServerInfo);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('system:info:list')")
    @GetMapping("/avatar")
    public void list(@RequestParam String url, HttpServletResponse response) {
        HttpURLConnection conn = null;
        try {
            URL imageUrl = new URL(url);
            conn = (HttpURLConnection) imageUrl.openConnection();
            conn.setRequestProperty("Referer", "https://www.bilibili.com");
            conn.setRequestMethod("GET");

            // 设置响应类型
            String contentType = conn.getContentType();
            response.setContentType(contentType != null ? contentType : "image/jpeg");

            // 复制图片数据
            try (InputStream in = conn.getInputStream();
                 OutputStream out = response.getOutputStream()) {
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = in.read(buffer)) != -1) {
                    out.write(buffer, 0, bytesRead);
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } catch (ProtocolException e) {
            throw new RuntimeException(e);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (conn != null) {
                conn.disconnect();
            }
        }
    }

    /**
     * 导出服务器信息主列表
     */
    @PreAuthorize("@ss.hasPermi('system:info:export')")
    @Log(title = "服务器信息主", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbServerInfo tbServerInfo)
    {
        List<TbServerInfo> list = tbServerInfoService.selectTbServerInfoList(tbServerInfo);
        ExcelUtil<TbServerInfo> util = new ExcelUtil<TbServerInfo>(TbServerInfo.class);
        util.exportExcel(response, list, "服务器信息主数据");
    }

    /**
     * 获取服务器信息主详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:info:query')")
    @GetMapping(value = "/{sid}")
    public AjaxResult getInfo(@PathVariable("sid") Long sid)
    {
        return success(tbServerInfoService.selectTbServerInfoBySid(sid));
    }

    /**
     * 新增服务器信息主
     */
    @PreAuthorize("@ss.hasPermi('system:info:add')")
    @Log(title = "服务器信息主", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbServerInfo tbServerInfo)
    {
        return toAjax(tbServerInfoService.insertTbServerInfo(tbServerInfo));
    }

    /**
     * 修改服务器信息主
     */
    @PreAuthorize("@ss.hasPermi('system:info:edit')")
    @Log(title = "服务器信息主", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbServerInfo tbServerInfo)
    {
        return toAjax(tbServerInfoService.updateTbServerInfo(tbServerInfo));
    }

    /**
     * 删除服务器信息主
     */
    @PreAuthorize("@ss.hasPermi('system:info:remove')")
    @Log(title = "服务器信息主", businessType = BusinessType.DELETE)
	@DeleteMapping("/{sids}")
    public AjaxResult remove(@PathVariable Long[] sids)
    {
        return toAjax(tbServerInfoService.deleteTbServerInfoBySids(sids));
    }
}
